﻿using jinmaMesProxy.DataAccess;
using jinmaMesProxy.Excep;
using jinmaMesProxy.Model;
using Microsoft.Data.SqlClient;
using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Text;

namespace jinmaMesProxy.Service
{
    public class MaterialService
    {
        ErpDbInstance dbInstance;
        PropertyInfo[] ps;
        static DataTable freeValues;
        ErpDocQuery<Material> query;
        public MaterialService()
        {
            dbInstance = ServiceLoader.GetService<ErpDbInstance>();
            ps = typeof(Material).GetProperties();
            query = new ErpDocQuery<Material>();
        }
        public List<Material> List(string updateTime)
        {
            List<Material> data = new List<Material>();
            var modifyName = typeof(Material).GetProperty(nameof(Material.ModifyDate))!
                .GetCustomAttribute<MyColumnNameAttribute>()!.ColumnName;
            return query.Query($"and {modifyName}>=@p1", new KeyValuePair<string, object>("@p1",updateTime));
        }
        public Material GetByCode(string code)
        {
            var col = typeof(Material).GetProperty(nameof(Material.MaterialCode))!
                .GetCustomAttribute<MyColumnNameAttribute>()!.ColumnName;
            var r = query.Query($"and {col}=@p1", new KeyValuePair<string, object>("@p1", code));
            if (r.Count == 0) throw new MyException($"找不到名为{code}的物料");
            else return r.First();
        }
        public void CheckFree<T>(Material material, T vouch)where T:DetailModel
        {
            var success= material.Free1 == !String.IsNullOrWhiteSpace(vouch.Free1)
                && material.Free2 == !String.IsNullOrWhiteSpace(vouch.Free2)
                && material.Free3 == !String.IsNullOrWhiteSpace(vouch.Free3)
                && material.Free4 == !String.IsNullOrWhiteSpace(vouch.Free4)
                && material.Free5 == !String.IsNullOrWhiteSpace(vouch.Free5)
                && material.Free6 == !String.IsNullOrWhiteSpace(vouch.Free6)
                && material.Free7 == !String.IsNullOrWhiteSpace(vouch.Free7)
                && material.Free8 == !String.IsNullOrWhiteSpace(vouch.Free8)
                && material.Free9 == !String.IsNullOrWhiteSpace(vouch.Free9)
                && material.Free10 == !String.IsNullOrWhiteSpace(vouch.Free10);
            if (!success) throw new MyException($"物料{material.MaterialName}的自由项设置错误");
        }

        public void CheckFreeValue<T>(T vouch) where T : DetailModel
        {
            if (freeValues == null)
            {
                var sql = "select citem,cvalue from userdefine a left join UserDef_Parent b on a.cid = b.cid where b.cclass = '存货' and b.citem like '自由项%'";
                using (var cmd = dbInstance.MakeQueryCommand(sql))
                {
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds);
                    freeValues = ds.Tables[0];
                }
            }
            for (int i = 1; i <= 10; i++)
            {
                var v = typeof(T).GetProperty($"Free{i}")!.GetValue(vouch);
                if (v == null) continue;
                var vs = v.ToString();
                if (vs!.ToString()=="") continue;
                var valid = freeValues.AsEnumerable()
                    .Where(r => r["citem"].ToString() == $"自由项{i}" && r["cvalue"].ToString() == vs)
                    .Count() > 0;
                if (!valid)
                    throw new MyException($"自由项{i}的值{vs}不合法");
            }
        }
    }
}
